<!--
 * @Descripttion:
 * @version:
 * @Author: cxguo
 * @Date: 2020-05-12 19:37:15
 * @LastEditors: cxguo
 * @LastEditTime: 2020-11-12 14:59:47
 -->
<template>
  <div>
    <vxe-grid
      ref="table"
      class="grid"
      border
      resizable
      highlight-hover-row
      :loading="tableLoading.table"
      :edit-config="{trigger: 'click', mode: 'row'}"
      :columns="columnsData"
      :data="tableData.table"
    />
  </div>
</template>

<script>
import { oftenuse } from '@/libs/mixins'
import { getStockGroupStoreByGood } from '@/api/stock/stock.js'

export default {
  name: 'GoodStock',
  mixins: [oftenuse],
  props: {
    skuId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      columnsData: [
        { field: 'canUseQty', title: '可用库存', width: 120,
          slots: {
            default: (params) => {
              const val = this.getCanUseQty(params.row || {})
              return [<span>{val}</span>]
            }
          }
        },
        { field: 'nowQty', title: '当前存货', width: 120,
          slots: {
            default: (params) => {
              const val = this.getNowQty(params.row || {})
              return [<span>{val}</span>]
            }
          }
        },
        { field: 'waitQuantityOut', title: '待出库量', width: 120 },
        { field: 'waitQuantityIn', title: '待入库量', width: 120 }
      ]
    }
  },
  created() {
    this.initData()
  },
  methods: {
    initData() {
      this.getStockGroupStoreByGood()
    },
    /**
     * 可用库存
     */
    getCanUseQty(row) {
      const { originQuantity, quantityIn, quantityOut, waitQuantityOut, waitQuantityIn } = row
      return this.$amount(originQuantity).add(quantityIn).add(waitQuantityIn)
        .subtract(quantityOut).subtract(waitQuantityOut).format()
    },
    /**
     * 当前存货
     */
    getNowQty(row) {
      const { originQuantity, quantityIn, quantityOut } = row
      return this.$amount(originQuantity).add(quantityIn)
        .subtract(quantityOut).format()
    },
    getStockGroupStoreByGood() {
      const skuId = this.skuId
      this.setTableLoading('table', true)
      getStockGroupStoreByGood(skuId).then(res => {
        this.setTableLoading('table', false)
        if (res.data.flag) {
          const data = res.data.data
          this.setTableData('table', data)
        }
      })
    }
  }
}
</script>

